package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Tag(name = "充值管理")
@RestController
@RequestMapping("api/account/rechargeInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class RechargeInfoApiController {

    @Autowired
    private RechargeInfoService rechargeInfoService;

    /**
     * 根据订单号获取充值信息
     *
     * @param orderNo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "根据订单号获取充值信息")
    @GetMapping("/getRechargeInfo/{orderNo}")
    public Result getRechargeInfo(@PathVariable String orderNo) {
        //  select * from recharge_info where order_no = ?
        return Result.ok(rechargeInfoService.getOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo, orderNo)));
    }

    /**
     * 给用户充值
     * @param rechargeInfoVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "充值")
    @PostMapping("submitRecharge")
    public Result submitRecharge(@RequestBody RechargeInfoVo rechargeInfoVo){
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法. recharge_info
        String orderNo = this.rechargeInfoService.submitRecharge(rechargeInfoVo,userId);
        //	前端与后台传递数据通常都是 key-value json 格式！ class Param{ private String orderNo;}
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderNo",orderNo);
        //  返回数据
        return Result.ok(map);
    }
}

